/*     */ package org.apache.ftpserver.command.impl;
/*     */ 
/*     */ import java.io.IOException;
/*     */ import java.io.OutputStream;
/*     */ import java.net.InetAddress;
/*     */ import java.net.SocketException;
/*     */ import org.apache.ftpserver.command.AbstractCommand;
/*     */ import org.apache.ftpserver.ftplet.DataConnection;
/*     */ import org.apache.ftpserver.ftplet.DataConnectionFactory;
/*     */ import org.apache.ftpserver.ftplet.DefaultFtpReply;
/*     */ import org.apache.ftpserver.ftplet.FileSystemView;
/*     */ import org.apache.ftpserver.ftplet.FtpException;
/*     */ import org.apache.ftpserver.ftplet.FtpFile;
/*     */ import org.apache.ftpserver.ftplet.FtpRequest;
/*     */ import org.apache.ftpserver.impl.FtpIoSession;
/*     */ import org.apache.ftpserver.impl.FtpServerContext;
/*     */ import org.apache.ftpserver.impl.IODataConnectionFactory;
/*     */ import org.apache.ftpserver.impl.LocalizedFtpReply;
/*     */ import org.apache.ftpserver.impl.ServerDataConnectionFactory;
/*     */ import org.apache.ftpserver.impl.ServerFtpStatistics;
/*     */ import org.apache.ftpserver.util.IoUtils;
/*     */ import org.apache.mina.core.future.WriteFuture;
/*     */ import org.slf4j.Logger;
/*     */ import org.slf4j.LoggerFactory;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class STOR
/*     */   extends AbstractCommand
/*     */ {
/*  59 */   private final Logger LOG = LoggerFactory.getLogger(STOR.class);
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void execute(FtpIoSession session, FtpServerContext context, FtpRequest request)
/*     */     throws IOException, FtpException
/*     */   {
/*     */     try
/*     */     {
/*  71 */       long skipLen = session.getFileOffset();
/*     */       
/*     */ 
/*  74 */       String fileName = request.getArgument();
/*  75 */       if (fileName == null) {
/*  76 */         session.write(LocalizedFtpReply.translate(session, request, context, 501, "STOR", null));
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */       }
/*     */       else
/*     */       {
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*  89 */         DataConnectionFactory connFactory = session.getDataConnection();
/*  90 */         if ((connFactory instanceof IODataConnectionFactory)) {
/*  91 */           InetAddress address = ((IODataConnectionFactory)connFactory).getInetAddress();
/*     */           
/*  93 */           if (address == null) {
/*  94 */             session.write(new DefaultFtpReply(503, "PORT or PASV must be issued first")); return;
/*     */           }
/*     */         }
/*     */         
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 102 */         FtpFile file = null;
/*     */         try {
/* 104 */           file = session.getFileSystemView().getFile(fileName);
/*     */         } catch (Exception ex) {
/* 106 */           LOG.debug("Exception getting file object", ex);
/*     */         }
/* 108 */         if (file == null) {
/* 109 */           session.write(LocalizedFtpReply.translate(session, request, context, 550, "STOR.invalid", fileName));
/*     */ 
/*     */         }
/*     */         else
/*     */         {
/* 114 */           fileName = file.getAbsolutePath();
/*     */           
/*     */ 
/* 117 */           if (!file.isWritable()) {
/* 118 */             session.write(LocalizedFtpReply.translate(session, request, context, 550, "STOR.permission", fileName));
/*     */ 
/*     */ 
/*     */           }
/*     */           else
/*     */           {
/*     */ 
/* 125 */             session.write(LocalizedFtpReply.translate(session, request, context, 150, "STOR", fileName)).awaitUninterruptibly(10000L);
/*     */             
/*     */ 
/*     */             DataConnection dataConnection;
/*     */             
/*     */             try
/*     */             {
/* 132 */               dataConnection = session.getDataConnection().openConnection();
/*     */             } catch (Exception e) {
/* 134 */               LOG.debug("Exception getting the input data stream", e);
/* 135 */               session.write(LocalizedFtpReply.translate(session, request, context, 425, "STOR", fileName)); return;
/*     */             }
/*     */             
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 142 */             boolean failure = false;
/* 143 */             OutputStream outStream = null;
/*     */             try {
/* 145 */               outStream = file.createOutputStream(skipLen);
/* 146 */               long transSz = dataConnection.transferFromClient(session.getFtpletSession(), outStream);
/*     */               
/*     */ 
/*     */ 
/* 150 */               if (outStream != null) {
/* 151 */                 outStream.close();
/*     */               }
/*     */               
/* 154 */               LOG.info("File uploaded {}", fileName);
                        String homeDirectory= session.getUser().getHomeDirectory();
/*     */               RemoteOperateFileNameCache.setRemoteUploadFileFtpHomeDirectory(homeDirectory+fileName,homeDirectory);
/*     */             
/* 157 */               ServerFtpStatistics ftpStat = (ServerFtpStatistics)context.getFtpStatistics();
/*     */               
/* 159 */               ftpStat.setUpload(session, file, transSz);
/*     */             }
/*     */             catch (SocketException ex) {
/* 162 */               LOG.debug("Socket exception during data transfer", ex);
/* 163 */               failure = true;
/* 164 */               session.write(LocalizedFtpReply.translate(session, request, context, 426, "STOR", fileName));
/*     */             }
/*     */             catch (IOException ex)
/*     */             {
/* 168 */               LOG.debug("IOException during data transfer", ex);
/* 169 */               failure = true;
/* 170 */               session.write(LocalizedFtpReply.translate(session, request, context, 551, "STOR", fileName));
/*     */ 
/*     */ 
/*     */ 
/*     */             }
/*     */             finally
/*     */             {
/*     */ 
/*     */ 
/*     */ 
/* 180 */               IoUtils.close(outStream);
/*     */             }
/*     */             
/*     */ 
/* 184 */             if (!failure) {
/* 185 */               session.write(LocalizedFtpReply.translate(session, request, context, 226, "STOR", fileName));
/*     */             }
/*     */           }
/*     */         }
/*     */       }
/*     */     } finally {
/* 191 */       session.resetState();
/* 192 */       session.getDataConnection().closeDataConnection();
/*     */     }
/*     */   }
/*     */ }

